<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ReMsg;
use App\Models\MsgBoard;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;

class MsgBoardController extends AdminController
{
    public function index(Content $content)
    {
        $tab = Tab::make();
        $tab->add('在线留言', $this->grid(), true);

        return $content->body($tab->withCard());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MsgBoard(['member']), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map))->orderByDesc('id');
            $grid->column('id');
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('member_info', '用户信息')->width('10%')->display(function (){
                return <<<HTML
<span>ID:{$this->member->id}</span><br>
<span>名称:{$this->member->nickname}</span><br>
<span>手机号:{$this->member->phone}</span><br>
HTML;

            });
            $grid->column('content');
            $grid->column('re_content');
            $grid->column('re_time')->display(function ($time){
                return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
            });
            $grid->column('state')->using(['未处理', '已处理'])->label(['danger', 'success']);
            $grid->column('created_at');

            $grid->disableCreateButton();
            $grid->disableEditButton();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (!$actions->row->state) {
                    $actions->append(new ReMsg(\App\Models\MsgBoard::class));
                }
            });

            $grid->filter(function (Grid\Filter $filter) use ($map) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->equal('member_id')->width(3);
                $filter->like('member.nickname', '用户名称')->width(3);
                $filter->like('content')->width(3);
                $filter->equal('state')->select([0 => '未处理', 1 => '已处理'])->width(3);
                $filter->between('created_at')->datetime()->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MsgBoard(), function (Show $show) {
            $show->field('id');
            $show->field('mini_type');
            $show->field('member_id');
            $show->field('content');
            $show->field('re_content');
            $show->field('re_time');
            $show->field('re_id');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MsgBoard(), function (Form $form) {
            $form->display('id');
            $form->text('mini_type');
            $form->text('member_id');
            $form->text('content');
            $form->text('re_content');
            $form->text('re_time');
            $form->text('re_id');
            $form->text('state');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
